package org.liuyunfeng.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @description:
 * @author: TanJingyu
 * @create: 2022-04-18 13:46
 **/
@RequestMapping("/json")
@RestController
public class JsonController {
    private ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/stages")
    public Object json() throws IOException {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("stages.json");
        return objectMapper.readTree(resourceAsStream);
    }

    @GetMapping("/types")
    public Object types() throws IOException {
//        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("types.json");
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/types.json");
        return objectMapper.readTree(resourceAsStream);
    }

    @GetMapping("/area-2")
    public Object area2() throws IOException {
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/areas-secondLevel.json");

        List<Old> olds = objectMapper.readValue(resourceAsStream, new TypeReference<List<Old>>() {
        });

        return olds;
    }


    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();

        Old old = new Old();

        System.out.println(objectMapper.writeValueAsString(old));

        System.out.println();
    }


    @Getter
    @Setter
    public static class Old {
        private Integer id;
        private String name;
        private List<Old> children;
    }
}

    